﻿using INTEX.DataBase;
using ReflectionStudio.Components.UserControls;
using ReflectionStudio.Core;
using ReflectionStudio.Core.Events;
using ReflectionStudio.Core.Helpers;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OracleClient;
using System.IO;
using System.Windows;
using System.Windows.Data;
using System.Linq;
using WPFExcelReport.Helpers;
namespace WPFExcelReport
{
    /// <summary>
    /// Interaction logic for ProductionOrder.xaml
    /// </summary>
    public partial class YCAFS05CHGS
    {
        #region-------------Properties-----------------------
        DataRow LastRow = null;
        DataSet SetMap = null; 
        YCAFS05CHGSData m_Data;

        YCAFS05CHGSReport m_rpt;

        public SETINTEXDB SetProd
        {
            get
            {
                return (SETINTEXDB)TryFindResource("SetProd");
            }
        }
        public SETINTEXDB SetData
        {
            get
            {
                return (SETINTEXDB)TryFindResource("SetData");
            }
        }

        CollectionViewSource vwABPOS
        {
            get
            {
                return (CollectionViewSource)this.TryFindResource("vwABPOS");
            }
        }
        DataRow dwABPOS
        {
            get
            {
                if (vwABPOS.View == null)
                    return null;
                if ((DataRowView)vwABPOS.View.CurrentItem == null)
                    return null;
                return ((DataRowView)vwABPOS.View.CurrentItem).Row;
            }
        }
        CollectionViewSource vwFDECK
        {
            get
            {
                return (CollectionViewSource)this.TryFindResource("vwFDECK");
            }
        }
        DataRow dwFDECK
        {
            get
            {
                if (vwFDECK.View == null)
                    return null;
                if ((DataRowView)vwFDECK.View.CurrentItem == null)
                    return null;
                return ((DataRowView)vwFDECK.View.CurrentItem).Row;
            }
        }
        CollectionViewSource vwWDECK
        {
            get
            {
                return (CollectionViewSource)this.TryFindResource("vwWDECK");
            }
        }
        DataRow dwWDECK
        {
            get
            {
                if (vwWDECK.View == null)
                    return null;
                if ((DataRowView)vwWDECK.View.CurrentItem == null)
                    return null;
                return ((DataRowView)vwWDECK.View.CurrentItem).Row;
            }
        }
        CollectionViewSource vwFSSTUELI
        {
            get
            {
                return (CollectionViewSource)this.TryFindResource("vwFSSTUELI");
            }
        }

        DataRow dwFSSTUELI
        {
            get
            {
                if (vwFSSTUELI.View == null)
                    return null;
                if ((DataRowView)vwFSSTUELI.View.CurrentItem == null)
                    return null;
                return ((DataRowView)vwFSSTUELI.View.CurrentItem).Row;
            }
        }
        CollectionViewSource vwTMPFSSTUELI
        {
            get
            {
                return (CollectionViewSource)this.TryFindResource("vwTMPFSSTUELI");
            }
        }

        DataRow dwTMPFSSTUELI
        {
            get
            {
                if (vwTMPFSSTUELI.View == null)
                    return null;
                if ((DataRowView)vwTMPFSSTUELI.View.CurrentItem == null)
                    return null;
                return ((DataRowView)vwTMPFSSTUELI.View.CurrentItem).Row;
            }
        }
        string ART
        {
            get
            {
                return m_Data.Left(txtMAT.Text.ToString() + "                      ", 20);
            }
        }

        BindingListCollectionView cvFS05
        {
            get
            {
                return (BindingListCollectionView)CollectionViewSource.GetDefaultView(dlgFSSTUELI.ItemsSource);
            }
        }
        string user = "";
        string FART
        {
            get
            {
                return m_Data.Left(txtFART.Text.ToString() + "                      ", 20);
            }
            set
            {
                txtFART.Text = value;
            }
        }
        #endregion
        public YCAFS05CHGS()
        {
            InitializeComponent();
            this.Name = "用量变更";
        }
        public override void SetUp(Dictionary<string, object> dicParamater)
        {
            dicParamater["blBuildCellMastrix"] = false;
            base.SetUp(dicParamater);
            InitializeComponent();
           
            m_Data = new YCAFS05CHGSData();
            user = (string)dicParamater["User"];
            txtMAT.Text = "BJCPW000108";
        }
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                if (IsLoaded)
                    return;
                IsLoaded = true;
                vwFSSTUELI.View.CurrentChanged += vwFSSTUELI_CurrentChanged;
                vwABPOS.View.CurrentChanged += vwABPOS_CurrentChanged;
                vwTMPFSSTUELI.View.CurrentChanged += vwTMPFSSTUELI_CurrentChanged;
                ResetSetInterface();
                m_Data.SetupUI();
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return; 
        }


        public void ResetSetInterface()
        {
            SetMap = new SETINTEXDB();
            DataSet setResult = new DataSet();
            string filePath = Path.Combine(PathHelper.ApplicationPath, "Report\\Interface\\FSCHGMAPINTEX.XML");
            setResult.ReadXml(filePath);
            SetMap.Merge(setResult);
            m_Data.SetCAD = SetMap;
            m_Data.SetExt = new DataSet();
        }
        void RefreshABAUFM(string krs, string lfd, string pos, string art, string var, string farbe, string size, string nqty, double bomrate)
        {
            try
            {
                var query = from DataRow row in SetProd.ICC_ABAUFM.AsEnumerable()
                            where row["ABAVKAUFKRS"].ToString() == krs
                            && row["ABAVKAUFLFD"].ToString() == lfd
                            && row["ABAVKAUFPOS"].ToString() == pos
                            && row["ABAART"].ToString() == art
                            && row["ABAVAR"].ToString().Trim() == var.Trim()
                            && row["ABAFARBE"].ToString() == farbe
                            && row["ABAGROE"].ToString() == size
                            select row;
                foreach (DataRow row in query)
                {
                    row["ABARESMG"] =nqty;
                    row["RATIO"] = bomrate;
                    m_Data.INTEXDBC.UpdateICC_ABAUFM(row, 3);
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        private void btnRefreshAB15_Click(object sender, RoutedEventArgs e)
        {
            if (dwABPOS == null)
            {
                MessageBox.Show("请选择生产单");
                return;
            } 
            if (dwTMPFSSTUELI == null)
            {
                MessageBox.Show("请选择其中一行BOM");
                return;
            }
            try
            {
                string abkrs = "", ablfd = "", abpos = "";
                string mart = "", mvar = "", mfarbe = "",msize="";
                abkrs = dwABPOS["ABPAUFKRS"].ToString();
                ablfd = dwABPOS["ABPAUFLFD"].ToString();
                abpos = dwABPOS["ABPAUFPOS"].ToString();
                mart = dwTMPFSSTUELI["FSSMART"].ToString();
                mvar = dwTMPFSSTUELI["FSSMVAR"].ToString();
                mfarbe = dwTMPFSSTUELI["FSSMFARBE"].ToString();
                msize = dwTMPFSSTUELI["FSSMGROE"].ToString();
                double req = 0;

                double FSSVBFKT = System.Convert.ToDouble(dwTMPFSSTUELI["FSSVBFKT"].ToString());
                double FSSVERBR = System.Convert.ToDouble(dwTMPFSSTUELI["FSSVERBR"].ToString());
                double FSSVERL = System.Convert.ToDouble(dwTMPFSSTUELI["FSSVERL"].ToString());
                double sdqty = 0;
                
                var qwd = from DataRow XD in SetProd.WDECK.AsEnumerable()
                          where XD["WDART"].ToString() == mart
                          && XD["WDVAR"].ToString().Trim()== mvar.Trim()
                          && XD["WDFARBE"].ToString() == mfarbe                         
                          select XD;
                foreach (DataRow rx in qwd)
                {
                    abpos = rx["WDAUFPOS"].ToString();
                    var qab = from DataRow row in SetData.ABPOS.AsEnumerable()
                              where row["ABPAUFPOS"].ToString() == abpos
                              select row;
                    foreach (DataRow row in qab)
                    {
                        req = Convert.ToDouble(row["ABPSOLLMENGE7"].ToString());
                    }
                    double bomrate = (FSSVERBR * FSSVBFKT * 0.000001 * (1 + FSSVERL * 0.0001)) * 0.001;
                    sdqty = req * bomrate;
                    rx["WDBEDARF"] = System.Math.Round(sdqty);
                    m_Data.INTEXDBC.UpdateWDECK(rx, "", 2);
                    RefreshABAUFM(abkrs, ablfd, abpos, mart, mvar, mfarbe, "       ", (sdqty / 100).ToString(), bomrate);
                }
                var qfd = from DataRow XD in SetProd.FDECK.AsEnumerable()
                          where XD["FDART"].ToString() == mart
                        && XD["FDVAR"].ToString().Trim() == mvar.Trim()
                        && XD["FDFARBE"].ToString() == mfarbe
                        && XD["FDGROE"].ToString() == msize
                          select XD;
                foreach (DataRow rx in qfd)
                {
                    abpos = rx["FDAUFPOS"].ToString();
                    var qab = from DataRow row in SetData.ABPOS.AsEnumerable()
                              where row["ABPAUFPOS"].ToString() == abpos
                              select row;
                    foreach (DataRow row in qab)
                    {
                        req = Convert.ToDouble(row["ABPSOLLMENGE7"].ToString());
                    }
                    double RATIO = 0.01 * (FSSVERBR * FSSVBFKT * 0.000001 * (1 + FSSVERL * 0.0001)) * 0.001;
                    sdqty = req*RATIO;
                    rx["FDMENGE"] = System.Math.Round(sdqty);
                    m_Data.INTEXDBC.UpdateFDECK(rx, "", 2);
                    RefreshABAUFM(abkrs, ablfd, abpos, mart, mvar, mfarbe, msize, rx["FDMENGE"].ToString(), RATIO);
                }
                //SetProd.AcceptChanges();
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        void vwFSSTUELI_CurrentChanged(object sender, EventArgs e)
        {
            if (dwFSSTUELI == null)
                return;
            if (LastRow == dwFSSTUELI)
                return;
            LastRow = dwFSSTUELI;
            try
            {
                txtFART.Text = dwFSSTUELI["FSSART"].ToString();
                string filter = "FSSART = '" + FART + "'";
                DataTable tb = SetData.FSSTUELI.Select(filter).CopyToDataTable();
                SetProd.FSSTUELI.Clear();
                SetProd.FSSTUELI.Merge(tb);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

        void vwABPOS_CurrentChanged(object sender, EventArgs e)
        {

            if (dwABPOS == null)
            {
                return;
            }
            if (LastRow == dwABPOS)
                return;
            LastRow = dwABPOS;
            if (dwTMPFSSTUELI == null)
            {
                return;
            }
            try
            {
                DataSet SResult = m_Data.GetData(dwABPOS, dwTMPFSSTUELI);
                SetProd.ICC_ABAUFM.Clear();
                SetProd.WDECK.Clear();
                SetProd.FDECK.Clear();
                SetProd.Merge(SResult.Tables["ICC_ABAUFM"]);
                SetProd.Merge(SResult.Tables["WDECK"]);
                SetProd.Merge(SResult.Tables["FDECK"]);      
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        void vwTMPFSSTUELI_CurrentChanged(object sender, EventArgs e)
        {
            if (dwTMPFSSTUELI == null)
                return;
            if (LastRow == dwTMPFSSTUELI)
                return;
            LastRow = dwTMPFSSTUELI;
            try
            {
                DataSet SResult = m_Data.GetSProdData(dwTMPFSSTUELI);
                SetData.ABPOS.Clear();
                SetData.ABPOS.Merge(SResult.Tables["ABPOS"]);
                if (vwABPOS.View != null)
                {
                    vwABPOS.View.MoveCurrentToFirst();
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        } 
      
        private void btnQuerySData_Click(object sender, EventArgs e)
        {
            try
            {
                cvFS05.CustomFilter = null;
                DataSet SResult = new DataSet();
                using (new LongOperation(this, "数据更新中"))
                {
                    SResult = m_Data.GetSData(ART);
                    SetData.Clear();
                    SetData.Merge(SResult);
                }
                if (vwFSSTUELI.View != null)
                {
                    vwFSSTUELI.View.MoveCurrentToFirst();
                }

            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        private void btnRefreshFS05_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (dwTMPFSSTUELI == null)
                    return;
                foreach (DataRow row in SetProd.FSSTUELI.Rows)
                {
                    m_Data.INTEXDBC.UpdateFSSTUELI(row, "", 2);
                }
                //cvFS05.CustomFilter = "FSSART= '" + FART + "' AND FSSMART = '" + ART + "'";
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        private void btnSaveDECK_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                foreach (DataRow row in SetProd.WDECK)
                {

                    m_Data.INTEXDBC.UpdateWDECK(row, "", 2);
                }
                foreach (DataRow row in SetProd.FDECK)
                {
                    m_Data.INTEXDBC.UpdateFDECK(row, "", 2);
                }
                foreach (DataRow row in SetProd.ICC_ABAUFM)
                {
                    m_Data.INTEXDBC.UpdateICC_ABAUFM(row, 3);
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
        private void btnFilterMat_Click(object sender, RoutedEventArgs e)
        {
            string filter = "FSSART = '" + FART + "'";
            cvFS05.CustomFilter = filter;
        }
        private void btnFilterMatClear_Click(object sender, RoutedEventArgs e)
        {
            cvFS05.CustomFilter = null;
        }

        private void btnSame_Click(object sender, RoutedEventArgs e)
        {
            if (dwTMPFSSTUELI == null)
                return;
            try
            {
                foreach (DataRow row in SetProd.FSSTUELI.Rows)
                {
                    row["FSSVERBR"] = dwTMPFSSTUELI["FSSVERBR"];
                    //m_Data.INTEXDBC.SaveFSSTUELI(row);
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

    }
}
